package cn.qsgame.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebView;

import java.util.List;

import cn.qsgame.base.entity.Function;
import cn.qsgame.base.internal.ICallback;

/**
 * @author #Suyghur.
 * Created on 4/22/21
 */
public class QSGameSdk {

    private Object mSdkBridgeMager = null;
    private String processName = "";

    private QSGameSdk() {

    }

    public static QSGameSdk getInstance() {
        return QSGameSdkHolder.INSTANCE;
    }

    private static final class QSGameSdkHolder {
        private static final QSGameSdk INSTANCE = new QSGameSdk();

        /**
         * 防止单例对象在反序列化时重新生成对象
         */
        private static Object readResolve() {
            return INSTANCE;
        }
    }

    private Object getSdkBridgeManager(Context context) {
        if (null == mSdkBridgeMager) {
            mSdkBridgeMager = SdkBridgeManager.initSdkBridgeManager(context);
        }
        return mSdkBridgeMager;
    }

    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (null == runningApps) {
            return "";
        }
        for (ActivityManager.RunningAppProcessInfo info : runningApps) {
            if (info.pid == android.os.Process.myPid()) {
                if (!TextUtils.isEmpty(info.processName)) {
                    return info.processName;
                }
            }
        }
        return "";
    }

    /**
     * 同步Application中的attachBaseContext
     */
    public void attachBaseContext(Application application, Context context) {
        if (TextUtils.isEmpty(processName)) {
            processName = getProcessName(context);
        }
        if (TextUtils.isEmpty(processName) || !processName.equals(context.getPackageName())) {
            return;
        }
        if (mSdkBridgeMager == null) {
            mSdkBridgeMager = getSdkBridgeManager(context);
        }
        SdkBridgeManager.call(Function.ATTACH_BASE_CONTEXT, new Class[]{Application.class, Context.class}, new Object[]{application, context});
    }

    /**
     * 同步Application中的onCreate
     */
    public void initApplication(Application application) {
        if (TextUtils.isEmpty(processName)) {
            processName = getProcessName(application);
        }
        if (TextUtils.isEmpty(processName) || !processName.equals(application.getPackageName())) {
            return;
        }
        //适配Android P WebView
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            try {
                if (!application.getPackageName().equals(processName)) {
//                    Logger.d("Android P 设置webView不同的目录");
                    WebView.setDataDirectorySuffix(processName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void initialize(Activity activity, boolean isLandscape, ICallback callback) {

    }

    public void login(Activity activity, ICallback callback) {

    }
}
